package com.wzy.business.store.me

import android.annotation.SuppressLint
import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import com.blankj.utilcode.util.EncryptUtils
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.wangzhenyu.common.base.BaseVBPActivity
import com.wangzhenyu.common.netcore.coroutine.CoroutineSupport
import com.wangzhenyu.common.util.exts.checkNotBlack
import com.wzy.business.R
import com.wzy.business.databinding.ActivityForgetPasswordBinding
import com.wzy.business.store.me.bean.ForgetPassword
import com.wzy.business.store.me.presenter.ForgetPasswordPresenter
import com.wzy.business.store.me.presenter.impl.ForgetPasswordPresenterImpl
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.Locale

class ForgetPasswordActivity :
    BaseVBPActivity<ActivityForgetPasswordBinding, ForgetPasswordPresenter>(),
    ForgetPasswordPresenter.View {


    private val mCoroutine: CoroutineSupport by lazy { CoroutineSupport() }

    override fun getViewBinding() = ActivityForgetPasswordBinding.inflate(layoutInflater)

    override fun useLightMode(): Boolean {
        return false
    }

    override fun initView() {
        mToolBarDelegate?.setMidTitle("忘记密码")
        mBinding.tvGetCode.setOnClickListener {
            if (TextUtils.isEmpty(mBinding.etPhone.text.toString())) {
                showToast("请输入手机号码")
                return@setOnClickListener
            }
            if (!RegexUtils.isMobileSimple(mBinding.etPhone.text.toString())) {
                showToast("请输入正确的手机号码")
                return@setOnClickListener
            }
            mPresenter?.requestSmsCodeInfoData(mBinding.etPhone.text.toString())
            doGetCodeTextAction()
        }

        mBinding.tvUpdatePasswordSubmit.setOnClickListener {
            try {
                checkNotBlack(mBinding.etPhone.text.toString()) { "请输入手机号码" }
                checkNotBlack(mBinding.etCode.text.toString()) { "请输入验证码" }
                checkNotBlack(mBinding.etPassword.text.toString()) { "请输入新密码" }
                check(RegexUtils.isMobileSimple(mBinding.etPhone.text.toString())) { "请输入正确的手机号码" }
                showDialogLoading()
                mPresenter?.requestPersonInfoData(
                    ForgetPassword(
                        mobile = mBinding.etPhone.text.toString(),
                        smsCode = mBinding.etCode.text.toString(),
                        password = EncryptUtils.encryptMD5ToString(mBinding.etPassword.text.toString())
                            .lowercase(
                                Locale.ROOT
                            )
                    )
                )
            } catch (e: IllegalStateException) {
                ToastUtils.showShort(e.message)
                e.printStackTrace()
            }
        }

        var showPassword = true
        mBinding.ivPasswordVisible.setOnClickListener {
            showPassword = !showPassword
            if (showPassword) {
                mBinding.etPassword.transformationMethod =
                    PasswordTransformationMethod.getInstance()
            } else {
                mBinding.etPassword.transformationMethod =
                    HideReturnsTransformationMethod.getInstance()
            }

        }
    }


    override fun createPresenter(): ForgetPasswordPresenter {
        return ForgetPasswordPresenterImpl(this, this)
    }


    @SuppressLint("SetTextI18n")
    private fun doGetCodeTextAction() {
        mBinding.tvGetCode.isEnabled = false
        mCoroutine.launch {
            var againTime = 60
            while (againTime > 0) {
                againTime -= 1
                mBinding.tvGetCode.text = againTime.toString() + "秒"
                delay(1000)
            }
            mBinding.tvGetCode.text = getString(R.string.get_code)
            mBinding.tvGetCode.isEnabled = true
        }
    }

    override fun onPersonInfoSuccess() {
        hideDialogLoading()
        showToast("修改密码成功,密码是1111")
        finish()
    }

    override fun onPersonInfoError() {
        hideDialogLoading()
    }

    override fun onSmsCodeInfoSuccess() {
        showToast("获取了验证码：1111")
    }

    override fun onSmsCodeInfoError() {

    }
}